package com.ly.system.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.ly.common.core.bean.R;
import com.ly.system.system.bean.dto.IdsDTO;
import com.ly.system.system.bean.entity.SysPermission;
import com.ly.system.system.bean.entity.SysPermissionDataRule;
import com.ly.system.system.bean.model.SysPermissionTree;
import com.ly.system.system.bean.vo.SysPermissionVO;
import com.ly.system.service.ISysPermissionDataRuleService;
import com.ly.system.service.ISysPermissionService;
import com.ly.system.service.ISysRolePermissionService;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 菜单权限
 *
 * @author luoyun
 * @since 2018-12-21
 */
@Slf4j
@RestController
@RequestMapping("/sys/permission")
@RequiredArgsConstructor
public class SysPermissionController {

    /**
     * 子菜单
     */
    private final ISysPermissionService sysPermissionService;
    private final ISysRolePermissionService sysRolePermissionService;
    private final ISysPermissionDataRuleService sysPermissionDataRuleService;

    /**
     * 加载数据节点
     *
     * @return
     */
    @GetMapping("/list")
    public R<List<SysPermissionTree>> list(SysPermission entity) {
        return sysPermissionService.getList(entity);
    }

    /**
     * 新增
     *
     * @param entity
     * @return
     */
    @PreAuthorize("@ss.hasPermission('system:permission:add')")
    @Operation(summary = "新增")
    @PostMapping("/add")
    public R<String> add(@RequestBody SysPermission entity) {
        return sysPermissionService.add(entity);
    }

    /**
     * 修改
     *
     * @param entity
     * @return
     */
    @PreAuthorize("@ss.hasPermission('system:permission:edit')")
    @Operation(summary = "修改")
    @PostMapping("/edit")
    public R<String> edit(@RequestBody SysPermission entity) {
        return sysPermissionService.edit(entity);
    }

    /**
     * 删除-id
     *
     * @param id
     * @return
     */
    @Operation(summary = "删除-id")
    @DeleteMapping("/delete")
    public R<String> delete(@RequestParam(name = "id") String id) {
        return sysPermissionService.delete(id);
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @Operation(summary = "批量删除")
    @DeleteMapping("/deleteBatch")
    public R<String> deleteBatch(@RequestParam(name = "ids") String ids) {
        String[] arr = ids.split(",");
        for (String id : arr) {
            if (StrUtil.isNotEmpty(id)) {
                sysPermissionService.delete(id);
            }
        }
        return R.ok();
    }

    /**
     * 分级获取菜单
     *
     * @return
     */
    @GetMapping("/getMenuListByParent")
    public R<List<SysPermissionTree>> getMenuListByParent(@RequestParam(name = "parentId", required = false) String parentId) {
        return sysPermissionService.getMenuListByParent(parentId);
    }

    /**
     * 查询用户拥有的菜单权限和按钮权限
     *
     * @return
     */
    @GetMapping("/getAuthByToken")
    public R<SysPermissionVO> getAuthByToken() {
        return sysPermissionService.getAuthByToken();
    }

    /**
     * 检测菜单路径是否存在
     *
     * @return
     */
    @GetMapping("/checkAuthUrlPath")
    public R<String> checkAuthUrlPath(SysPermission entity) {
        boolean check = sysPermissionService.checkAuthUrlPath(entity);
        return check ? R.ok("该值可用！") : R.fail("访问路径不允许重复，请重定义！");
    }

    /**
     * 查询角色授权
     *
     * @return
     */
    @GetMapping("/getAuthByRoleId")
    public R<List<String>> getAuthByRole(@RequestParam(name = "roleId") String roleId) {
        return sysRolePermissionService.getAuthByRoleId(roleId);
    }

    /**
     * 保存角色授权
     *
     * @return
     */
    @PostMapping("/saveRoleAuth")
    public R<String> saveRoleAuth(@Validated @RequestBody IdsDTO dto) {
        return sysRolePermissionService.saveRoleAuth(dto);
    }

    /**
     * 添加菜单权限数据
     *
     * @param sysPermissionDataRule
     * @return
     */
    @PostMapping("/addPermissionRule")
    public R<String> addPermissionRule(@RequestBody SysPermissionDataRule sysPermissionDataRule) {
        sysPermissionDataRule.setCreateTime(new Date());
        sysPermissionDataRuleService.savePermissionDataRule(sysPermissionDataRule);
        return R.ok("操作成功！");
    }

    @PostMapping("/editPermissionRule")
    public R<String> editPermissionRule(@RequestBody SysPermissionDataRule sysPermissionDataRule) {
        sysPermissionDataRuleService.saveOrUpdate(sysPermissionDataRule);
        return R.ok("操作成功！");
    }

    /**
     * 删除菜单权限数据
     *
     * @param id
     * @return
     */
    @DeleteMapping("/deletePermissionRule")
    public R<String> deletePermissionRule(@RequestParam(name = "id") String id) {
        sysPermissionDataRuleService.deletePermissionDataRule(id);
        return R.ok("删除成功！");
    }

    /**
     * 查询菜单权限数据
     *
     * @param entity
     * @return
     */
    @GetMapping("/getAuthDataRule")
    public R<List<SysPermissionDataRule>> getAuthDataRule(SysPermissionDataRule entity) {
        List<SysPermissionDataRule> list = sysPermissionDataRuleService.getAuthDataRule(entity);
        return R.ok(list);
    }

    /**
     * 部门权限表
     *
     * @param departId
     * @return
     */
    @GetMapping("/queryDepartPermission")
    public R<List<String>> queryDepartPermission(@RequestParam(name = "departId", required = true) String departId) {
        return R.ok();
    }

    /**
     * 保存部门授权
     *
     * @return
     */
    @PostMapping("/saveDepartPermission")
    public R<String> saveDepartPermission(@RequestBody JSONObject json) {
        return R.ok("保存成功！");
    }
}
